﻿namespace WinForms
{
    using Bll.Systems;
    using Model.Systems;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;
    using WinForms.Properties;
    using WinFormSession;

    public class FrmLogin : Form
    {
        private Button btn_Login;
        private string codeSerial = "0,1,2,3,4,5,6,7,8,9";
        private IContainer components = null;
        private Label label1;
        private Label label2;
        private Label lbl_BanQuan;
        private Label lblCode;
        private Label lblLogin;
        private int length = 4;
        private Panel pan_Login;
        private Panel pan_Login2;
        private Panel panel1;
        private PictureBox pic_Logo;
        private PictureBox pictureBox2;
        private TextBox txt_Code;
        private TextBox txt_LoginName;
        private TextBox txt_PassWord;

        public FrmLogin()
        {
            this.InitializeComponent();
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        public string CreateVerifyCode()
        {
            return this.CreateVerifyCode(0);
        }

        public string CreateVerifyCode(int codeLen)
        {
            if (codeLen == 0)
            {
                codeLen = this.Length;
            }
            string[] strArray = this.CodeSerial.Split(new char[] { ',' });
            string str = "";
            int index = -1;
            Random random = new Random((int) DateTime.Now.Ticks);
            for (int i = 0; i < codeLen; i++)
            {
                index = random.Next(0, strArray.Length - 1);
                str = str + strArray[index];
            }
            return str;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.lblCode.Text = this.CreateVerifyCode();
            base.AcceptButton = this.btn_Login;
            base.Activate();
        }

        private void InitializeComponent()
        {
            ComponentResourceManager resources = new ComponentResourceManager(typeof(FrmLogin));
            this.pan_Login = new Panel();
            this.pictureBox2 = new PictureBox();
            this.pic_Logo = new PictureBox();
            this.panel1 = new Panel();
            this.lbl_BanQuan = new Label();
            this.btn_Login = new Button();
            this.lblCode = new Label();
            this.txt_Code = new TextBox();
            this.txt_PassWord = new TextBox();
            this.txt_LoginName = new TextBox();
            this.label2 = new Label();
            this.label1 = new Label();
            this.lblLogin = new Label();
            this.pan_Login2 = new Panel();
            this.pan_Login.SuspendLayout();
            ((ISupportInitialize) this.pictureBox2).BeginInit();
            ((ISupportInitialize) this.pic_Logo).BeginInit();
            this.panel1.SuspendLayout();
            base.SuspendLayout();
            this.pan_Login.BackgroundImage = Resources.login_bg;
            this.pan_Login.Controls.Add(this.pictureBox2);
            this.pan_Login.Controls.Add(this.pic_Logo);
            this.pan_Login.Controls.Add(this.panel1);
            this.pan_Login.Controls.Add(this.pan_Login2);
            this.pan_Login.Dock = DockStyle.Fill;
            this.pan_Login.Location = new Point(0, 0);
            this.pan_Login.Name = "pan_Login";
            this.pan_Login.Size = new Size(0x3f8, 0x234);
            this.pan_Login.TabIndex = 1;
            this.pictureBox2.BackColor = Color.FromArgb(0xe2, 0xf9, 0xe5);
            this.pictureBox2.BackgroundImage = Resources.logo_11;
            this.pictureBox2.Location = new Point(0x23, 0xa1);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new Size(0x153, 8);
            this.pictureBox2.TabIndex = 3;
            this.pictureBox2.TabStop = false;
            this.pic_Logo.BackColor = Color.FromArgb(0xe2, 0xf9, 0xe5);
            this.pic_Logo.BackgroundImage = Resources.logo;
            this.pic_Logo.Location = new Point(0x23, 0x73);
            this.pic_Logo.Name = "pic_Logo";
            this.pic_Logo.Size = new Size(0x112, 0x23);
            this.pic_Logo.TabIndex = 2;
            this.pic_Logo.TabStop = false;
            this.panel1.BackgroundImage = (Image) resources.GetObject("panel1.BackgroundImage");
            this.panel1.Controls.Add(this.lbl_BanQuan);
            this.panel1.Controls.Add(this.btn_Login);
            this.panel1.Controls.Add(this.lblCode);
            this.panel1.Controls.Add(this.txt_Code);
            this.panel1.Controls.Add(this.txt_PassWord);
            this.panel1.Controls.Add(this.txt_LoginName);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.lblLogin);
            this.panel1.Location = new Point(0x23, 0xcc);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(460, 0xfb);
            this.panel1.TabIndex = 1;
            this.lbl_BanQuan.AutoSize = true;
            this.lbl_BanQuan.BackColor = Color.FromArgb(0, 0, 15, 0xff);
            this.lbl_BanQuan.ForeColor = Color.FromArgb(0x1c, 0x56, 0);
            this.lbl_BanQuan.Location = new Point(0x8a, 0xb6);
            this.lbl_BanQuan.Name = "lbl_BanQuan";
            this.lbl_BanQuan.Size = new Size(0xb9, 12);
            this.lbl_BanQuan.TabIndex = 9;
            this.lbl_BanQuan.Text = "磐嘉软件 版权所有(C) 2009-2012";
            this.btn_Login.FlatAppearance.BorderSize = 0;
            this.btn_Login.FlatStyle = FlatStyle.Flat;
            this.btn_Login.Image = Resources.login_go;
            this.btn_Login.Location = new Point(0x145, 50);
            this.btn_Login.Name = "btn_Login";
            this.btn_Login.Size = new Size(50, 0x2b);
            this.btn_Login.TabIndex = 8;
            this.btn_Login.UseVisualStyleBackColor = true;
            this.btn_Login.Click += new EventHandler(this.pic_btnLogin_Click);
            this.lblCode.AutoSize = true;
            this.lblCode.BackColor = Color.FromArgb(230, 0xf3, 0xe1);
            this.lblCode.Font = new Font("宋体", 10.5f);
            this.lblCode.ForeColor = Color.FromArgb(0x1c, 0x56, 0);
            this.lblCode.Location = new Point(240, 0x75);
            this.lblCode.Name = "lblCode";
            this.lblCode.Size = new Size(0x31, 14);
            this.lblCode.TabIndex = 7;
            this.lblCode.Text = "验证码";
            this.txt_Code.Font = new Font("宋体", 10f);
            this.txt_Code.Location = new Point(0x8b, 0x71);
            this.txt_Code.MaxLength = 4;
            this.txt_Code.Name = "txt_Code";
            this.txt_Code.Size = new Size(0x58, 0x17);
            this.txt_Code.TabIndex = 3;
            this.txt_PassWord.Font = new Font("宋体", 10f);
            this.txt_PassWord.Location = new Point(0x8b, 0x4f);
            this.txt_PassWord.MaxLength = 12;
            this.txt_PassWord.Name = "txt_PassWord";
            this.txt_PassWord.PasswordChar = '*';
            this.txt_PassWord.Size = new Size(0xa5, 0x17);
            this.txt_PassWord.TabIndex = 2;
            this.txt_LoginName.Font = new Font("宋体", 10f);
            this.txt_LoginName.Location = new Point(0x8b, 0x2d);
            this.txt_LoginName.MaxLength = 12;
            this.txt_LoginName.Name = "txt_LoginName";
            this.txt_LoginName.Size = new Size(0xa5, 0x17);
            this.txt_LoginName.TabIndex = 1;
            this.label2.AutoSize = true;
            this.label2.BackColor = Color.FromArgb(0xee, 0xf6, 0xe9);
            this.label2.Font = new Font("宋体", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.label2.ForeColor = Color.FromArgb(0x1c, 0x56, 0);
            this.label2.Location = new Point(0x52, 0x75);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x3f, 14);
            this.label2.TabIndex = 12;
            this.label2.Text = "验证码：";
            this.label1.AutoSize = true;
            this.label1.BackColor = Color.FromArgb(0xee, 0xf6, 0xe9);
            this.label1.Font = new Font("宋体", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.label1.ForeColor = Color.FromArgb(0x1c, 0x56, 0);
            this.label1.Location = new Point(0x51, 0x54);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x3f, 14);
            this.label1.TabIndex = 11;
            this.label1.Text = "密  码：";
            this.lblLogin.AutoSize = true;
            this.lblLogin.BackColor = Color.FromArgb(0xee, 0xf6, 0xe9);
            this.lblLogin.Font = new Font("宋体", 10.5f, FontStyle.Regular, GraphicsUnit.Point, 0x86);
            this.lblLogin.ForeColor = Color.FromArgb(0x1c, 0x56, 0);
            this.lblLogin.Location = new Point(0x51, 50);
            this.lblLogin.Name = "lblLogin";
            this.lblLogin.Size = new Size(0x3f, 14);
            this.lblLogin.TabIndex = 10;
            this.lblLogin.Text = "用户名：";
            this.pan_Login2.BackgroundImage = Resources.login_bg1;
            this.pan_Login2.Location = new Point(0x1f2, 0);
            this.pan_Login2.Name = "pan_Login2";
            this.pan_Login2.Size = new Size(510, 0x22b);
            this.pan_Login2.TabIndex = 0;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            base.ClientSize = new Size(0x3f8, 0x234);
            base.Controls.Add(this.pan_Login);
            base.Icon = (Icon)resources.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmLogin";
            this.Text = "茶叶进销存管理系统[标准版](登录)";
            base.Load += new EventHandler(this.FrmLogin_Load);
            base.FormClosing += new FormClosingEventHandler(this.FrmLogin_FormClosing);
            this.pan_Login.ResumeLayout(false);
            ((ISupportInitialize) this.pictureBox2).EndInit();
            ((ISupportInitialize) this.pic_Logo).EndInit();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            base.ResumeLayout(false);
        }

        private void pic_btnLogin_Click(object sender, EventArgs e)
        {
            if (this.lblCode.Text.Trim() != this.txt_Code.Text.Trim())
            {
                MessageBox.Show("请输入正确的验证码!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else if ((this.txt_LoginName.Text.Trim() != "") && (this.txt_PassWord.Text.Trim() != ""))
            {
                if (UserBll.CheckUserPassWinform(this.txt_LoginName.Text.Trim(), this.txt_PassWord.Text.Trim()))
                {
                    UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
                    PublicAppliction.LoginUser = ent;
                    ChainEnt model = ChainBll.GetModel(ent.ChainNum);
                    if ((model != null) && (model.IsUse == 0))
                    {
                        MessageBox.Show("对不起,您所在的分店已被停用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else
                    {
                        WorkLogBll.AddNewLog("成功登录系统", "", PublicAppliction.LoginUser);
                        base.Close();
                        base.DialogResult = DialogResult.OK;
                    }
                }
                else
                {
                    MessageBox.Show("用户名或者密码错误,请重新登录一次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            else
            {
                MessageBox.Show("请填写用户名或密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        public string CodeSerial
        {
            get
            {
                return this.codeSerial;
            }
            set
            {
                this.codeSerial = value;
            }
        }

        public int Length
        {
            get
            {
                return this.length;
            }
            set
            {
                this.length = value;
            }
        }
    }
}

